package com.example.booksystem.controller;

import com.example.booksystem.Result.Result;
import com.example.booksystem.constant.Constant;
import com.example.booksystem.model.UserInfo;
import com.example.booksystem.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Result getUser(String name, String password, HttpSession session){
        log.info("日志的打印 name:{},password:{}",name,password);

        if(!StringUtils.hasLength(name)&&!StringUtils.hasLength(password)){
            return Result.fail("账户密码不能为空");
        }
        UserInfo userInfo = userService.getUserService(name);
        if(userInfo==null){
            return Result.fail("用户不存在");
        }
        if(userInfo.getPassWord().equals(password)){
            session.setAttribute(Constant.USER_SESSION_KEY,userInfo);   //设置session
            return Result.success("");
        }
        return Result.fail("账号或密码错误");



    }
}
